package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.http.entity.CustomListBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.utils.ValidateUtils;
import com.weimei.conversionTreasure.weight.BottomDelPop;

import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class KhRecordVM extends BaseViewModel<Repository> {

    public ObservableInt emptyView = new ObservableInt(View.GONE);
    public ObservableInt jxsRecy = new ObservableInt(View.GONE);
    public ObservableInt khRecy = new ObservableInt(View.GONE);

    public ObservableField<String> type = new ObservableField<>("");

    public KhRecordVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity = activity;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (type.get().equals("0")){
            jxsRecy.set(View.VISIBLE);
            khRecy.set(View.GONE);
            getJXSData();
        }else {
            jxsRecy.set(View.GONE);
            khRecy.set(View.VISIBLE);
            getKHData();
        }


    }


    public ObservableList<KhItemVM> observableListKh = new ObservableArrayList<>();
    public ItemBinding<KhItemVM> itemBindingKh = ItemBinding.of(BR.viewModel, R.layout.item_kh);

    public ObservableList<KhItemVM> observableListJxs = new ObservableArrayList<>();
    public ItemBinding<KhItemVM> itemBindingJxs = ItemBinding.of(BR.viewModel, R.layout.item_jxs);

    private void getJXSData() {
        observableListJxs.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.dealerlist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<CustomListBean>() {
                    @Override
                    public void onNext(CustomListBean result) {
                        if (result.issucc){
                                if (ValidateUtils.isNotEmptyObjectOrString(result.getData())){
                                    if (result.getData().size()>0){
                                        emptyView.set(View.GONE);
                                        for (CustomListBean.DataDTO jsxData : result.getData()) {
                                            KhItemVM jsxItem = new KhItemVM(KhRecordVM.this);
                                            jsxItem.setJxsData(jsxData);
                                            jsxItem.setFlag(type.get());
                                            observableListJxs.add(jsxItem);
                                        }
                                    }else {
                                        emptyView.set(View.VISIBLE);
                                    }
                                }

                        }else {
                            emptyView.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    private void getKHData() {
        observableListKh.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        model.customlist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<CustomListBean>() {
                    @Override
                    public void onNext(CustomListBean result) {
                        if (result.issucc){
                            if (ValidateUtils.isNotEmptyObjectOrString(result.getData())){
                                if (result.getData().size()>0){
                                    emptyView.set(View.GONE);
                                    for (CustomListBean.DataDTO khData : result.getData()) {
                                        KhItemVM khItem = new KhItemVM(KhRecordVM.this);
                                        khItem.setKhData(khData);
                                        khItem.setFlag(type.get());
                                        observableListKh.add(khItem);
                                    }
                                }else {
                                    emptyView.set(View.VISIBLE);
                                }
                            }
                        }else {
                            emptyView.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void delData(int id){
        new XPopup.Builder(mActivity)
                .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .asCustom(new BottomDelPop(mActivity, s1 -> {
                    delList(id);
                },"删除记录"))
                .show();



    }

    private void delList(int id) {
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid", id+"");
        model.delete_custom(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult result) {
                        if (result.issucc){
                            onResume();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
